package com.navinfo.tripanalysis.common.util;

import com.navinfo.tripanalysis.common.arithmetic.common.EngineAggregatedData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;

/**
 * @Description: 计算发动机数据各项指标精准时长工具类
 * @author 刘俊
 */
public class FixEngineAggregatedDataUtils {

    /**
     * 根据行程积分时长跟行程精准时长比例，计算出缺项和异常项的精准时长
     * @param statisticData
     * @param engineData
     */
    public static void fixEngineAggregatedData(OuterStatisticData statisticData, EngineAggregatedData engineData) {
        int routeDuration = (int)(engineData.getRouteEndTime() - engineData.getRouteStartTime());
        long exactRouteStartTime = statisticData.getRouteStartTime();
        long exactRouteEndTime = statisticData.getRouteEndTime();
        int exactRouteDuration = (int) (exactRouteEndTime - exactRouteStartTime);
        engineData.setRouteStartTime(exactRouteStartTime);
        engineData.setRouteEndTime(exactRouteEndTime);
        engineData.setRouteDuration(exactRouteDuration);
        engineData.setTripMileage(statisticData.getTripMileage());
        engineData.setTripId(statisticData.getTripId());
        //根据：单项精准时长 = 行程精准时长 * 单项积分时长 / 行程积分时长 等式，计算出各个缺项和异常项的精确时长
        //设置缺项精准时长
        engineData.setSpeedMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getSpeedMissDuration()));
        engineData.setBarometricPressureMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getBarometricPressureMissDuration()));
        engineData.setOutputTorqueMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getOutputTorqueMissDuration()));
        engineData.setFrictionTorqueMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getFrictionTorqueMissDuration()));
        engineData.setRotationMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getRotationMissDuration()));
        engineData.setFuelConsumptionMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getFuelConsumptionMissDuration()));
        engineData.setScrUpNOxMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrUpNOxMissDuration()));
        engineData.setScrDownNOxMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrDownNOxMissDuration()));
        engineData.setReagentAllowanceMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getReagentAllowanceMissDuration()));
        engineData.setAirInflowMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getAirInflowMissDuration()));
        engineData.setScrInMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrInMissDuration()));
        engineData.setScrOutMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrOutMissDuration()));
        engineData.setDropoutVoltageMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getDropoutVoltageMissDuration()));
        engineData.setCoolantTemperatureMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getCoolantTemperatureMissDuration()));
        engineData.setTankLevelMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getTankLevelMissDuration()));
        engineData.setPositioningStateMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getPositioningStateMissDuration()));
        engineData.setLongitudeMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getLongitudeMissDuration()));
        engineData.setLatitudeMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getLatitudeMissDuration()));
        engineData.setMileageMissDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getMileageMissDuration()));
        //设置异常项精准时长
        engineData.setSpeedAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getSpeedAbnmDuration()));
        engineData.setBarometricPressureAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getBarometricPressureAbnmDuration()));
        engineData.setOutputTorqueAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getOutputTorqueAbnmDuration()));
        engineData.setFrictionTorqueAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getFrictionTorqueAbnmDuration()));
        engineData.setRotationAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getRotationAbnmDuration()));
        engineData.setFuelConsumptionAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getFuelConsumptionAbnmDuration()));
        engineData.setScrUpNOxAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrUpNOxAbnmDuration()));
        engineData.setScrDownNOxAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrDownNOxAbnmDuration()));
        engineData.setReagentAllowanceAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getReagentAllowanceAbnmDuration()));
        engineData.setAirInflowAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getAirInflowAbnmDuration()));
        engineData.setScrInAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrInAbnmDuration()));
        engineData.setScrOutAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getScrOutAbnmDuration()));
        engineData.setDropoutVoltageAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getDropoutVoltageAbnmDuration()));
        engineData.setCoolantTemperatureAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getCoolantTemperatureAbnmDuration()));
        engineData.setTankLevelAbnmDuration(getCorrectResult(routeDuration, exactRouteDuration, engineData.getTankLevelAbnmDuration()));
    }

    /**
     * 获取原始值对应的精准值
     * @param oldTotalValue 原始总和
     * @param correctTotalValue 精准总和
     * @param oldSingleValue    原始值
     * @return
     */
    private static Integer getCorrectResult(int oldTotalValue, int correctTotalValue, Integer oldSingleValue) {
        if (oldSingleValue == null ) {
            return null;
        }
        if (oldTotalValue == correctTotalValue) {
            return oldSingleValue;
        }
        if (oldTotalValue <= 0) {
            return 0;
        }

        return (int) ( (1.0 * oldSingleValue / oldTotalValue) * correctTotalValue );
    }

}
